package com.controller;

import com.pojo.User;
import com.service.UserService;
import com.util.MD5Util;
import com.util.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: HL
 * Date: 2020/3/17
 * Time: 9:59
 * Description: No Description
 */
@Api(tags = "用户的API")
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    private RedisTemplate<String,Object> redisTemplate;
    @ApiOperation(value ="创建用户",notes ="创建用户" )
    @PostMapping("/user")
    @ResponseBody
    public ServerResponse add(User user){
        // ServerResponse serverResponse =
        //System.out.println("serverResponse:"+serverResponse);
        return userService.save(user);
    }
    /*
      根据id删除用户，假删除状态改为0
   */
    @ApiOperation(value = "根据用户的id删除用户",notes ="修改用户的状态")
    @DeleteMapping("/user/{id}")
    @ResponseBody
    public ServerResponse del(@PathVariable("id")String id){
        ServerResponse serverResponse = userService.modifyStatus(Integer.valueOf(id));
        System.out.println("serverResponse:"+serverResponse);
        return serverResponse;
    }
    /*
      修改用户信息
   */
    @ApiOperation(value ="修改用户信息",notes ="修改用户信息" )
    @PutMapping("/user")
    @ResponseBody
    public ServerResponse upd(User user){
        System.out.println(user);
        ServerResponse serverResponse = userService.modify(user);
        return serverResponse;
    }
    /*
      根据id查询用户
   */
    @ApiOperation(value ="根据用户id查询用户信息")
    @GetMapping("/user/{id}")
    @ResponseBody
    public ServerResponse findById(@PathVariable("id") String id){
        System.out.println(id);
        ServerResponse serverResponse = userService.getById(Integer.valueOf(id));
        return serverResponse;
    }

    @ApiOperation(value ="查询用户信息")
    @GetMapping("/user")
    @ResponseBody
    public ServerResponse findAll(){
        ServerResponse serverResponse = userService.getAll();
        return serverResponse;
    }
    /*
     跳转用户管理主页面
    */
    @ApiOperation(value = "获得管理员端的主页面",notes = "管理员端")
    @GetMapping("/user/backhome")
    public String goUserBackHome(Model model){
        ServerResponse serverResponse = userService.getAll();
        model.addAttribute("info",serverResponse.getData());
        return "back/user/userbackhome";
    }

    @GetMapping("user/backlogin")
    @ResponseBody
    public ServerResponse backLogin(User user, HttpSession session){
        System.out.println(user.getUserName()+":"+user.getUserPsd());

        if (user.getUserName()==null||user.getUserName().equals("")||!user.getUserName().equals("admin")){
            return ServerResponse.getDataFailed("账号错误");
        }else if (user.getUserPsd()==null||user.getUserPsd().equals("")||!user.getUserPsd().equals("admin")){
            return ServerResponse.getDataFailed("密码错误");
        }else {
            session.setAttribute("backuser", user);
            return ServerResponse.getDataSuccess("登录成功");
        }
        //return new Result(true,"登陆成功", CodeStatus.OK,user);
    }
    @ApiOperation(value = "短信验证",notes = "")
    @PostMapping("/user/phonecode")
    @ResponseBody
    public ServerResponse getPhoneCode(String telno){
        if (telno == null){
            return ServerResponse.getDataFailed("请输入手机号");
        }else {
            ServerResponse serverResponse = userService.getByTelno(telno);
            System.out.println(serverResponse);
            if (serverResponse.getStatus()==1){//表示手机号已经注册过了
                return ServerResponse.hasRegist();
            }else {
                //调用短信接口
//               String randomCode = AliyunCodeUtil.getRandomCode();
//               AliyunCode.sendMessage(telno,randomCode);
                String randomCode = "6666";
                return ServerResponse.getCodeSuccess(randomCode);
            }
        }
    }
    @ApiOperation(value = "注册",notes = "")
    @PostMapping("/user/register")
    @ResponseBody
    public ServerResponse register(String telno, String password){
        if (userService.getByTelno(telno).getStatus()==0){//
            User user = new User();
            user.setUserTelno(telno);
            user.setUserPsd(MD5Util.md5(password));
            user.setCreatetime(new Date());
            ServerResponse serverResponse = userService.save(user);
            return serverResponse;
        }
        return ServerResponse.hasRegist();
    }
    @ApiOperation(value = "客户端登录",notes = "")
    @PostMapping("/user/login")
    @ResponseBody
    public ServerResponse login(String telno, String password){
        ServerResponse serverResponse = userService.getTelnoAndPassword(telno, password);
        System.out.println(serverResponse);
        if (serverResponse.getStatus() == 1){//表示用户登录成功
            //生成Token数据库
            String token = UUID.randomUUID()+"";
            //存到redis数据库中
//           redisTemplate.opsForValue().set(token,serverResponse.getData(), Duration.ofHours(1));//存放在redis中一个小时

            return serverResponse;
        }
        return ServerResponse.getDataFailed("用户不存在或已登录");
    }
  /*  @ApiOperation(value = "客户端退出",notes = "")
    @PostMapping("/user/quit")
    @ResponseBody
    public ServerResponse quit(User user){
        if (user.getType() == 1) //表示用户已登录

    }
*/

}
